//
//  FMDeviceTool.m
//  flutter_device_info_plugin
//

#import "FMDeviceTool.h"
#import "FMKeychainTool.h"
#import <sys/utsname.h>
#import <AdSupport/ASIdentifierManager.h>
#import <SystemConfiguration/CaptiveNetwork.h>
#import <NetworkExtension/NetworkExtension.h>

@implementation FMDeviceTool

+ (NSString *)getFMIDFA {
    NSString *idfa = [[[NSUserDefaults standardUserDefaults] objectForKey:KFMTrackingInfo] description];
    if (idfa.length == 0) {
        idfa = [[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString];
    }
    return idfa;
}

+ (NSString *)getFMIDFV {
    NSString *data = [FMKeychainTool getData];
    if (data.length != 0) {
        return data;
    }else {
        NSString *idfv = [[UIDevice currentDevice].identifierForVendor UUIDString];
        [FMKeychainTool saveData:idfv];
        return idfv;
    }
}

+ (NSString *)getFMAppVersion {
    NSString *appversion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    return appversion;
}

+ (NSString *)getFMSystemVersion {
    NSString *sysVersion = [[UIDevice currentDevice] systemVersion];
    return sysVersion;
}

+ (NSString *)getFMSystemName {
    NSString *sysName = [[UIDevice currentDevice] systemName];
    return sysName;
}

+ (NSString *)getOriginalDeviceInfo {
    struct utsname originalDeviceInfo;
    uname(&originalDeviceInfo);
    NSString *deviceInfo = [NSString stringWithCString:originalDeviceInfo.machine encoding:NSUTF8StringEncoding];
    return deviceInfo;
}

+ (NSString *)getDeviceNameplate {
    NSString *deviceInfo = [self getOriginalDeviceInfo];
    
    if ([deviceInfo isEqualToString:@"iPhone7,1"]) {
        return @"iPhone 6 Plus";
    } else if ([deviceInfo isEqualToString:@"iPhone7,2"]) {
        return @"iPhone 6";
    } else if ([deviceInfo isEqualToString:@"iPhone8,1"]) {
        return @"iPhone 6s";
    } else if ([deviceInfo isEqualToString:@"iPhone8,2"]) {
        return @"iPhone 6s Plus";
    } else if ([deviceInfo isEqualToString:@"iPhone8,4"]) {
        return @"iPhone SE";
    } else if ([deviceInfo isEqualToString:@"iPhone9,1"] || [deviceInfo isEqualToString:@"iPhone9,3"]) {
        return @"iPhone 7";
    } else if ([deviceInfo isEqualToString:@"iPhone9,2"] || [deviceInfo isEqualToString:@"iPhone9,4"]) {
        return @"iPhone 7 Plus";
    } else if ([deviceInfo isEqualToString:@"iPhone10,1"] || [deviceInfo isEqualToString:@"iPhone10,4"]) {
        return @"iPhone 8";
    } else if ([deviceInfo isEqualToString:@"iPhone10,2"] || [deviceInfo isEqualToString:@"iPhone10,5"]) {
        return @"iPhone 8 Plus";
    } else if ([deviceInfo isEqualToString:@"iPhone10,3"] || [deviceInfo isEqualToString:@"iPhone10,6"]) {
        return @"iPhone X";
    } else if ([deviceInfo isEqualToString:@"iPhone11,2"]) {
        return @"iPhone XS";
    } else if ([deviceInfo isEqualToString:@"iPhone11,4"] || [deviceInfo isEqualToString:@"iPhone11,6"]) {
        return @"iPhone XS Max";
    } else if ([deviceInfo isEqualToString:@"iPhone11,8"]) {
        return @"iPhone XR";
    } else if ([deviceInfo isEqualToString:@"i386"] || [deviceInfo isEqualToString:@"x86_64"]) {
        return @"Simulator";
    } else {
        return deviceInfo;
    }
}

+ (NSString *)getLocalTimeZone {
    NSTimeZone *zone = [NSTimeZone localTimeZone];
    NSInteger secondsFromGMT = [zone secondsFromGMT];
    NSString *gmtValue = [NSString stringWithFormat:@"GMT%+ld", (long)(secondsFromGMT / 3600)];
    return gmtValue;
}

+ (NSString *)getLocalLanguageCode {
    NSArray *preferredLanguages = [NSLocale preferredLanguages];
    NSString *languageIdentifier = preferredLanguages.firstObject ?: @"en";
    return languageIdentifier;
}

+ (NSDictionary *)getWifiInfo {
    NSArray *interfaces = (__bridge NSArray *)CNCopySupportedInterfaces();
    
    NSString *SSID = @"";
    NSString *BSSID = @"";
    for (NSString *interface in interfaces) {
        NSDictionary *info = (__bridge NSDictionary *)CNCopyCurrentNetworkInfo((__bridge CFStringRef)interface);
        if (info) {
            if ([info objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID]) {
                SSID = [info objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID];
                BSSID = [info objectForKey:(__bridge NSString *)kCNNetworkInfoKeyBSSID];
            }
        }
    }
    
    return @{
        @"SSID" : SSID,
        @"BSSID" : BSSID
    };
}

+ (BOOL)isFMSimulator {
    BOOL flag = TARGET_IPHONE_SIMULATOR == 1 && TARGET_OS_IPHONE == 1;
    return flag;
}

+ (BOOL)isPhoneJailbreak {
    BOOL hasExists = [[NSFileManager defaultManager] fileExistsAtPath:@"/User/Applications/"];
    return hasExists;
}

@end
